const { DataTypes } = require('sequelize');
const sequelize = require('../config/database');

const TradingOrders = sequelize.define('TradingOrders', {
  id: {
    type: DataTypes.BIGINT,
    allowNull: false,
    autoIncrement: true,
    comment: '订单ID',
  },
  userId: {
    type: DataTypes.BIGINT,
    allowNull: false,
    comment: '用户ID',
  },
  strategyId: {
    type: DataTypes.BIGINT,
    comment: '策略ID',
  },
  okxOrderId: {
    type: DataTypes.STRING(50),
    comment: 'OKX订单ID',
  },
  symbol: {
    type: DataTypes.STRING(20),
    allowNull: false,
    comment: '交易对',
  },
  side: {
    type: DataTypes.ENUM,
    allowNull: false,
    comment: '买卖方向',
  },
  orderType: {
    type: DataTypes.ENUM,
    allowNull: false,
    comment: '订单类型',
  },
  price: {
    type: DataTypes.DECIMAL(20, 8),
    comment: '价格',
  },
  quantity: {
    type: DataTypes.DECIMAL(20, 8),
    allowNull: false,
    comment: '数量',
  },
  filledQuantity: {
    type: DataTypes.DECIMAL(20, 8),
    defaultValue: 0.00000000,
    comment: '已成交数量',
  },
  status: {
    type: DataTypes.ENUM,
    defaultValue: pending,
    comment: '订单状态',
  },
  fee: {
    type: DataTypes.DECIMAL(20, 8),
    defaultValue: 0.00000000,
    comment: '手续费',
  },
  feeCurrency: {
    type: DataTypes.STRING(10),
    comment: '手续费币种',
  },
  executedAt: {
    type: DataTypes.DATE,
    comment: '成交时间',
  },
  deletedAt: {
    type: DataTypes.DATE,
    comment: '删除时间',
  },
  createdAt: {
    type: DataTypes.DATE,
    defaultValue: CURRENT_TIMESTAMP,
    comment: '创建时间',
  },
  updatedAt: {
    type: DataTypes.DATE,
    defaultValue: CURRENT_TIMESTAMP,
    comment: '更新时间',
  }
}, {
  tableName: 'trading_orders',
  comment: '交易订单表',
  timestamps: true,
  paranoid: true,
  underscored: true,
  createdAt: 'created_at',
  updatedAt: 'updated_at',
  deletedAt: 'deleted_at'
});

module.exports = TradingOrders;
